﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSGui.Classes.BusinessLogic;


namespace POSGui
{
    public struct SystemVariable
    {
        //public List<Permission> SystemPermissions;
        //public List<Role> SystemRoles;
        //public Cashier Cashier;
        //public PosUser User;
        public String LastPostbackID;
    }

    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
        }

        private bool login(string username, string password, string branch)
        {
            WebServiceAgent serviceAgent = ClientManager.GetInstance().LogIn(username, password, branch);
            if (serviceAgent == null)
            {
                LoginUser.FailureText = Constants.USER_ERROR;
                return false;
            }
            else
            {                         
                Session.Add(Constants.USER_WEB_SERVICE_CONN, serviceAgent);
                return true;
            }
        }

        /// <summary>
        /// Método que se encarga de crear la variable de sesion para el usuario y de redireccionarlo 
        /// a la página correspondiente, según el tipo de usuario.
        /// </summary>
        /// <param name="sender">LoginUser</param>
        /// <param name="e">Click</param>
        protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
        {
            e.Authenticated = login(LoginUser.UserName, LoginUser.Password, Constants.COMPANY_NAME);

            if (e.Authenticated)
            {
                var strRedirect = Request["ReturnUrl"] ?? Constants.PAGE_MENU;
                Response.Redirect(strRedirect, true);
            }
        }        

    }
}